package lg.minecraft.plugin.cobblemoncore.util;

import com.cobblemon.mod.common.pokemon.Pokemon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
public class PokemonPlaceholderReplacer {

    private PokemonPlaceholderReplacer() {}

    /**
     * 替换字符串中的 {key} 占位符为对应的值。
     *
     * @param input 模板字符串
     * @param placeholders 键值对 Map，包含要替换的占位符和对应值
     * @return 替换后的字符串
     */
    public static String replacePlaceholders(String input, Map<String, String> placeholders) {
        if (input == null) return "";
        if (!input.matches(".*\\{.+}.*")) return input;
        for (Map.Entry<String, String> map : placeholders.entrySet()) {
            String key = map.getKey();
            String value = map.getValue();
            if (key == null) {
                continue;
            }
            input = input.replace(key, value);
        }
        return input;
    }

    /**
     * 对列表中的每个字符串进行占位符替换。
     *
     * @param inputList 字符串列表
     * @param placeholders 占位符键值对
     * @return 替换后的字符串列表
     */
    public static List<String> replacePlaceholders(List<String> inputList, Map<String, String> placeholders) {
        if (inputList == null) return List.of();
        List<String> result = new ArrayList<>();
        for (String line : inputList) {
            result.add(replacePlaceholders(line, placeholders));
        }
        return result;
    }

    /**
     * 构建包含宝可梦信息的占位符映射。
     *
     * @param pokemon 宝可梦对象
     * @return 占位符键值对
     */
    public static Map<String, String> buildPokemonInfoPlaceholders(Pokemon pokemon) {
        Map<String, String> placeholders = new HashMap<>();

        // 1. 基础信息
        placeholders.put("{pokemon_name}", PokemonUtil.getSpeciesName(pokemon));
        placeholders.put("{nick_name}", PokemonUtil.getNickName(pokemon));
        placeholders.put("{level}", PokemonUtil.getLevelToString(pokemon));
        placeholders.put("{poke_dex}", PokemonUtil.getNationalPokedexIntegerToString(pokemon));
        placeholders.put("{gender}", PokemonUtil.getGenderName(pokemon));
        placeholders.put("{scale}", PokemonUtil.getScaleToString(pokemon));
        placeholders.put("{nature}", PokemonUtil.getNatureName(pokemon));
        placeholders.put("{mint_nature}", PokemonUtil.getMintedNatureName(pokemon));
        placeholders.put("{nature_type}", PokemonUtil.getNatureType(pokemon));
        placeholders.put("{mint_nature_type}", PokemonUtil.getMintedNatureType(pokemon));
        placeholders.put("{ability}", PokemonUtil.getAbilityName(pokemon));
        placeholders.put("{shiny}", PokemonUtil.isShinyToString(pokemon));
        placeholders.put("{uuid}", PokemonUtil.getUUIDToString(pokemon));
        placeholders.put("{form_name}", PokemonUtil.getFormName(pokemon));

        // 2. 战斗属性 - 个体值 (IV)
        placeholders.put("{ivs_hp}", PokemonUtil.getIvHPToString(pokemon));
        placeholders.put("{ivs_attack}", PokemonUtil.getIvAttackToString(pokemon));
        placeholders.put("{ivs_defence}", PokemonUtil.getIvDefenceToString(pokemon));
        placeholders.put("{ivs_special_attack}", PokemonUtil.getIvSpecialAttackToString(pokemon));
        placeholders.put("{ivs_special_defence}", PokemonUtil.getIvSpecialDefenceToString(pokemon));
        placeholders.put("{ivs_speed}", PokemonUtil.getIvSpeedToString(pokemon));
        placeholders.put("{ivs_sum}", PokemonUtil.getIvSumToString(pokemon));

        // 3. 努力值 (EV)
        placeholders.put("{evs_hp}", PokemonUtil.getEvHPToString(pokemon));
        placeholders.put("{evs_attack}", PokemonUtil.getEvAttackToString(pokemon));
        placeholders.put("{evs_defence}", PokemonUtil.getEvDefenceToString(pokemon));
        placeholders.put("{evs_special_attack}", PokemonUtil.getEvSpecialAttackToString(pokemon));
        placeholders.put("{evs_special_defence}", PokemonUtil.getEvSpecialDefenceToString(pokemon));
        placeholders.put("{evs_speed}", PokemonUtil.getEvSpeedToString(pokemon));
        placeholders.put("{evs_sum}", PokemonUtil.getEvSumToString(pokemon));

        // 4. 当前能力值 (Stat)
        placeholders.put("{stat_hp}", PokemonUtil.getStatHPToString(pokemon));
        placeholders.put("{stat_attack}", PokemonUtil.getStatAttackToString(pokemon));
        placeholders.put("{stat_defence}", PokemonUtil.getStatDefenceToString(pokemon));
        placeholders.put("{stat_special_attack}", PokemonUtil.getStatSpecialAttackToString(pokemon));
        placeholders.put("{stat_special_defence}", PokemonUtil.getStatSpecialDefenceToString(pokemon));
        placeholders.put("{stat_speed}", PokemonUtil.getStatSpeedToString(pokemon));

        // 5. 基础能力值 (Base Stat)
        placeholders.put("{base_stat_hp}", PokemonUtil.getBaseStatHPToString(pokemon));
        placeholders.put("{base_stat_attack}", PokemonUtil.getBaseStatAttackToString(pokemon));
        placeholders.put("{base_stat_defence}", PokemonUtil.getBaseStatDefenceToString(pokemon));
        placeholders.put("{base_stat_special_attack}", PokemonUtil.getBaseStatSpecialAttackToString(pokemon));
        placeholders.put("{base_stat_special_defence}", PokemonUtil.getBaseStatSpecialDefenceToString(pokemon));
        placeholders.put("{base_stat_speed}", PokemonUtil.getBaseStatSpeedToString(pokemon));
        placeholders.put("{base_stats_sum}", PokemonUtil.getBaseStatSumToString(pokemon));

        // 6. 技能信息
        placeholders.put("{move1}", PokemonUtil.getMoveName(pokemon, 0));
        placeholders.put("{move2}", PokemonUtil.getMoveName(pokemon, 1));
        placeholders.put("{move3}", PokemonUtil.getMoveName(pokemon, 2));
        placeholders.put("{move4}", PokemonUtil.getMoveName(pokemon, 3));

        placeholders.put("{type1}", PokemonUtil.getPrimaryTypeName(pokemon));
        placeholders.put("{type2}", PokemonUtil.getSecondaryTypeName(pokemon));

        // 7. 其他状态信息
        placeholders.put("{held_item}", PokemonUtil.getHeldItemName(pokemon));
        placeholders.put("{ball}", PokemonUtil.getCaughtBallName(pokemon));
        placeholders.put("{catch}", PokemonUtil.getCatchRateToString(pokemon));
        placeholders.put("{friendship}", PokemonUtil.getFriendshipToString(pokemon));
        placeholders.put("{trade}", PokemonUtil.getTradeAbleToString(pokemon));
        placeholders.put("{exp}", PokemonUtil.getExpToString(pokemon));
        placeholders.put("{experience_to_level_up}", PokemonUtil.getExperienceToLevelUpToString(pokemon));
        placeholders.put("{dmax_level}", PokemonUtil.getDmaxLevelToString(pokemon));
        placeholders.put("{owner_name}", PokemonUtil.getOwnerPlayerName(pokemon));
        placeholders.put("{original_trainer_name}", BasePokemonUtil.getOriginalTrainerName(pokemon));

        return placeholders;
    }

}
